/*
Copyright (C) 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

function Country() {
  this.name = '';
  this.shortName = '';
  this.rank = null;
  this.goldCount = 0;
  this.silverCount = 0;
  this.bronzeCount = 0;
}

Country.prototype.getTotalCount = function() {
  return this.goldCount + this.silverCount + this.bronzeCount;
};

Country.buildName = function(code) {
  return strings[code];
};

Country.prototype.getName = function() {
  return Country.buildName(this.shortName);
};

Country.FLAG_FOLDER_PATH = 'flags/';

Country.buildFlagPath = function(code) {
  return Country.FLAG_FOLDER_PATH + code + '.gif';
};

Country.prototype.getFlagPath = function() {
  return Country.buildFlagPath(this.shortName);
};

Country.countries = null;

Country.loadCountries = function() {
  var countries = [];

  for (var i = 0; i < Country.COUNTRY_DATA.length; ++i) {
    var data = Country.COUNTRY_DATA[i];
    var country = new Country();
    country.shortName = data[0];
    country.name = data[1];
    countries.push(country);
  }

  return countries;
};

Country.getCountries = function() {
  if (!Country.countries) {
    Country.countries = Country.loadCountries();
  }

  return Country.countries;
};

Country.getCountriesSortedByName = function() {
  var countries = Country.getCountries().slice(0);

  countries.sort(function(a, b) {
    var x = a.name;
    var y = b.name;

    return x < y ?
        -1 :
         x > y ? 1 : 0;
  });

  return countries;
};

Country.findCodeByName = function(name) {
  var countries = Country.getCountries();

  for (var i = 0; i < countries.length; ++i) {
    if (countries[i].name == name) {
      return countries[i].shortName;
    }
  }

  return '';
};

Country.COUNTRY_DATA = [
['AFG', strings.AFG],
['AHO', strings.AHO],
['ALB', strings.ALB],
['ALG', strings.ALG],
['AND', strings.AND],
['ANG', strings.ANG],
['ANT', strings.ANT],
['ARG', strings.ARG],
['ARM', strings.ARM],
['ARU', strings.ARU],
['ASA', strings.ASA],
['AUS', strings.AUS],
['AUT', strings.AUT],
['AZE', strings.AZE],
['BAH', strings.BAH],
['BAN', strings.BAN],
['BAR', strings.BAR],
['BDI', strings.BDI],
['BEL', strings.BEL],
['BEN', strings.BEN],
['BER', strings.BER],
['BHU', strings.BHU],
['BIH', strings.BIH],
['BIZ', strings.BIZ],
['BLR', strings.BLR],
['BOL', strings.BOL],
['BOT', strings.BOT],
['BRA', strings.BRA],
['BRN', strings.BRN],
['BRU', strings.BRU],
['BUL', strings.BUL],
['BUR', strings.BUR],
['CAF', strings.CAF],
['CAM', strings.CAM],
['CAN', strings.CAN],
['CAY', strings.CAY],
['CGO', strings.CGO],
['CHA', strings.CHA],
['CHI', strings.CHI],
['CHN', strings.CHN],
['CIV', strings.CIV],
['CMR', strings.CMR],
['COD', strings.COD],
['COK', strings.COK],
['COL', strings.COL],
['COM', strings.COM],
['CPV', strings.CPV],
['CRC', strings.CRC],
['CRO', strings.CRO],
['CUB', strings.CUB],
['CYP', strings.CYP],
['CZE', strings.CZE],
['DEN', strings.DEN],
['DJI', strings.DJI],
['DMA', strings.DMA],
['DOM', strings.DOM],
['ECU', strings.ECU],
['EGY', strings.EGY],
['ERI', strings.ERI],
['ESA', strings.ESA],
['ESP', strings.ESP],
['EST', strings.EST],
['ETH', strings.ETH],
['FIJ', strings.FIJ],
['FIN', strings.FIN],
['FRA', strings.FRA],
['FSM', strings.FSM],
['GAB', strings.GAB],
['GAM', strings.GAM],
['GBR', strings.GBR],
['GBS', strings.GBS],
['GEO', strings.GEO],
['GEQ', strings.GEQ],
['GER', strings.GER],
['GHA', strings.GHA],
['GRE', strings.GRE],
['GRN', strings.GRN],
['GUA', strings.GUA],
['GUI', strings.GUI],
['GUM', strings.GUM],
['GUY', strings.GUY],
['HAI', strings.HAI],
['HKG', strings.HKG],
['HON', strings.HON],
['HUN', strings.HUN],
['INA', strings.INA],
['IND', strings.IND],
['IRI', strings.IRI],
['IRL', strings.IRL],
['IRQ', strings.IRQ],
['ISL', strings.ISL],
['ISR', strings.ISR],
['ISV', strings.ISV],
['ITA', strings.ITA],
['IVB', strings.IVB],
['JAM', strings.JAM],
['JOR', strings.JOR],
['JPN', strings.JPN],
['KAZ', strings.KAZ],
['KEN', strings.KEN],
['KGZ', strings.KGZ],
['KIR', strings.KIR],
['KOR', strings.KOR],
['KSA', strings.KSA],
['KUW', strings.KUW],
['LAO', strings.LAO],
['LAT', strings.LAT],
['LBA', strings.LBA],
['LBR', strings.LBR],
['LCA', strings.LCA],
['LES', strings.LES],
['LIB', strings.LIB],
['LIE', strings.LIE],
['LTU', strings.LTU],
['LUX', strings.LUX],
['MAD', strings.MAD],
['MAR', strings.MAR],
['MAS', strings.MAS],
['MAW', strings.MAW],
['MDA', strings.MDA],
['MDV', strings.MDV],
['MEX', strings.MEX],
['MGL', strings.MGL],
['MHL', strings.MHL],
['MKD', strings.MKD],
['MLI', strings.MLI],
['MLT', strings.MLT],
['MNE', strings.MNE],
['MON', strings.MON],
['MOZ', strings.MOZ],
['MRI', strings.MRI],
['MTN', strings.MTN],
['MYA', strings.MYA],
['NAM', strings.NAM],
['NCA', strings.NCA],
['NED', strings.NED],
['NEP', strings.NEP],
['NGR', strings.NGR],
['NIG', strings.NIG],
['NOR', strings.NOR],
['NRU', strings.NRU],
['NZL', strings.NZL],
['OMA', strings.OMA],
['PAK', strings.PAK],
['PAN', strings.PAN],
['PAR', strings.PAR],
['PER', strings.PER],
['PHI', strings.PHI],
['PLE', strings.PLE],
['PLW', strings.PLW],
['PNG', strings.PNG],
['POL', strings.POL],
['POR', strings.POR],
['PRK', strings.PRK],
['PUR', strings.PUR],
['QAT', strings.QAT],
['ROU', strings.ROU],
['RSA', strings.RSA],
['RUS', strings.RUS],
['RWA', strings.RWA],
['SAM', strings.SAM],
['SEN', strings.SEN],
['SEY', strings.SEY],
['SIN', strings.SIN],
['SKN', strings.SKN],
['SLE', strings.SLE],
['SLO', strings.SLO],
['SMR', strings.SMR],
['SOL', strings.SOL],
['SOM', strings.SOM],
['SRB', strings.SRB],
['SRI', strings.SRI],
['STP', strings.STP],
['SUD', strings.SUD],
['SUI', strings.SUI],
['SUR', strings.SUR],
['SVK', strings.SVK],
['SWE', strings.SWE],
['SWZ', strings.SWZ],
['SYR', strings.SYR],
['TAN', strings.TAN],
['TGA', strings.TGA],
['THA', strings.THA],
['TJK', strings.TJK],
['TKM', strings.TKM],
['TLS', strings.TLS],
['TOG', strings.TOG],
['TPE', strings.TPE],
['TRI', strings.TRI],
['TUN', strings.TUN],
['TUR', strings.TUR],
['TUV', strings.TUV],
['UAE', strings.UAE],
['UGA', strings.UGA],
['UKR', strings.UKR],
['URU', strings.URU],
['USA', strings.USA],
['UZB', strings.UZB],
['VAN', strings.VAN],
['VEN', strings.VEN],
['VIE', strings.VIE],
['VIN', strings.VIN],
['YEM', strings.YEM],
['ZAM', strings.ZAM],
['ZIM', strings.ZIM]];
